package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.DishExistException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: HF62sky
 * @package: com.sky.service.impl
 * @className: DishServiceImpl
 * @author: SunGang
 * @description: TODO
 * @date: 2023/12/5 14:17
 * @version: 1.0
 */
@Service
@RequiredArgsConstructor
@Transactional
public class DishServiceImpl implements DishService {
    private final DishMapper dishMapper;
    private final DishFlavorMapper dishFlavorMapper;
    private final SetmealDishMapper setmealDishMapper;

    @Override
    public void add(DishDTO dishDTO) {
        //判断名称是否重复
        Dish dishQuery = new Dish();
        dishQuery.setName(dishDTO.getName());
        Integer countByName = dishMapper.selectCount(dishQuery);
        if (countByName > 0) {
            throw new DishExistException("菜品名称重复,请调整");
        }
        //添加彩屏到数据库
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.insert(dish);
        //添加口味到数据库
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() != 0) {
            flavors.forEach(c -> c.setDishId(dish.getId()));
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dishQuery = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dishQuery);
        Page<Dish> list = dishMapper.selectPage(dishQuery);
        long total = list.getTotal();
        List<DishVO> dishVOS = list.getResult().stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            return dishVO;
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult();
        pageResult.setRecords(dishVOS);
        pageResult.setTotal(total);
        return pageResult;

    }


    @Override
    @Transactional
    public void delete(List<String> ids) {
        Integer count = dishMapper.selectCountById(ids, StatusConstant.ENABLE);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

        }
        Integer count2 = setmealDishMapper.selectByDishIds(ids);
        if (count2 > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        }
        dishMapper.deleteByIds(ids);
        ids.forEach(id -> dishFlavorMapper.delete(Long.parseLong(id)));
    }

    @Override
    public DishVO queryId(Long id) {
        Dish dish = dishMapper.seletById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.seleceByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void modify(DishDTO dishDTO) {
        //去重
        Dish dishQuery = new Dish();
        dishQuery.setName(dishDTO.getName());
        dishQuery.setId(dishDTO.getId());
        Integer countByName = dishMapper.selectCount(dishQuery);
        if (countByName > 0) {
            throw new DishExistException("菜品名称重复,请调整");
        }
        //修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateById(dish);
        //修改口味
        dishFlavorMapper.delete(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(f -> f.setDishId(dishDTO.getId()));
        }
        dishFlavorMapper.insert(flavors);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> dishes = dishMapper.selet(categoryId);
        return dishes;
    }
}
